home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / v10n15.arc / VECTORS.CPP < prev    next >
Text File  |  1991-08-09  |  2KB  |  68 lines

  1. // VECTORS.CPP
  2. // Definitions of Member Functions for Vector Class
  3. // Copyright (C) 1991 Ziff Davis Communications
  4. // PC Magazine * Ray Duncan
  5.  
  6. #include <math.h>
  7. #include <iostream.h>
  8. #include "vectors.h"
  9.  
  10.  
  11. // display magnitude and direction (in degrees) of vector
  12. void VECTOR::display(void)
  13. {
  14.     cout << "magnitude=" << magnitude << " " ;
  15.     cout << "direction=" << rad2deg(direction) << " " ;
  16. }
  17.  
  18.  
  19. // initialize magnitude and direction of vector
  20. void VECTOR::set(double magMsg, double dirMsg)
  21. {
  22.     direction = deg2rad(dirMsg);            // initialize direction
  23.     magnitude = magMsg;                     // initialize magnitude
  24. }
  25.  
  26.  
  27. // add another vector's magnitude and direction to this vector
  28. void VECTOR::add(double magMsg, double dirMsg)
  29. {
  30.     double angle;                           // scratch storage
  31.  
  32.     dirMsg = deg2rad(dirMsg);               // convert argument to radians
  33.     angle = dirMsg - direction;             // find angle between vectors
  34.  
  35.     if (angle == pi)                        // special handling to avoid
  36.     {                                       // overflow for angle=180
  37.         direction = magnitude > magMsg ? direction : dirMsg;
  38.         magnitude = fabs(magnitude - magMsg);
  39.         return;
  40.     }
  41.  
  42.     if ((magnitude == 0) && (magMsg == 0))  // special handling to avoid
  43.     {                                       // divide by zero exception
  44.         magnitude = 0;                      // if both vectors have 
  45.         direction = 0;                      // zero magnitude
  46.         return;
  47.     }
  48.                                             // calculate resultant vector
  49.     magnitude = sqrt((magnitude * magnitude) + (magMsg * magMsg) +
  50.                      (2 * magnitude * magMsg * cos(angle)));
  51.     direction = direction + asin(((magMsg * sin(angle))/magnitude));
  52.     return;                                 
  53. }
  54.  
  55.  
  56. // private member function to convert degress to radians
  57. double VECTOR::deg2rad(double degrees)
  58. {
  59.     return ((degrees * 2 * pi)/360);
  60. }
  61.  
  62.  
  63. // private member function to convert radians to degrees
  64. double VECTOR::rad2deg(double radians)
  65. {
  66.     return ((radians * 360)/(2 * pi));
  67. }
  68.